package cn.stylefeng.guns.modular.util;

import cn.stylefeng.guns.core.enums.PaymentTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.enums.ReturnCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


/**
 * @author by Sgmder 2017/1/18.
 */
public class BaseUtils {

    /**
     * 有些SERVICE需要
     */
    public static String JsonString(ReturnCode code, Object data) {
        return JSON.toJSONString(new ResponseData(code, data));
    }

    public static String ResultString(ReturnCode code, Object data, String str){
        return JSON.toJSONString(new ResponseData(code, data, str));
    }

    //设置状态列表
    public static List<Integer> handleStatusList(String statusList){
        JSONArray statusArray = JSON.parseArray(statusList);
        List<Integer> statuses = null;
        if(null != statusArray){
            statuses = statusArray.toJavaList(Integer.class);
        }
        if(null != statuses && statuses.isEmpty()){
            statuses = null;
        }
        return statuses;
    }

    //将JSON字符串转换成list
    public static <T> List<T> JsonStringToList(String str, Class<T> clazz){
        JSONArray jsonArray = JSON.parseArray(str);
        List<T> list;
        if(null != jsonArray){
            list = jsonArray.toJavaList(clazz);
        }else {
            list = null;
        }
        return list;
    }

    //将JSON字符串转换成实体对象列表
    public static <T> List<T> JsonStringToListObject(String str, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        JSONArray jsonArray = JSON.parseArray(str);
        if(jsonArray != null){
            for (int i=0; i<jsonArray.size(); i++){
                JSONObject object = jsonArray.getJSONObject(i);
                T t = JSONObject.parseObject(object.toString(), clazz);
                list.add(t);
            }
        }
        return list;
    }

    public static String getIDCardWatermarketImage(String sourceUrl) {
        //水印图片 BASE64Encoder加密部分 logo/watermark/FXIDcard.png?x-oss-process=image/resize,P_70
        return sourceUrl + "?x-oss-process=image/watermark,image_bG9nby93YXRlcm1hcmsvRlhJRGNhcmQucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfNzA=,t_90,g_center,x_10,y_10";
    }

    /**
     * 订单指派时是否指派了司机
     * @param driverId
     * @param driverName
     * @param driverPhone
     * @param driverCardNumber
     * @param licensePlateNumber
     * @return
     */
    public static boolean hasAssignDriver(Long driverId, String driverName, String driverPhone, String driverCardNumber, String licensePlateNumber) {
        if (driverId==null) {
            return true;
        }
        if (StringUtils.isNotBlank(driverName) && StringUtils.isNotBlank(driverPhone)) {
            return true;
        }
        return false;
    }

    public static boolean listIsAll(List<Integer> orderStatusList, Integer target) {
        for (Integer status : orderStatusList) {
            if (!status.equals(target)) {
                return false;
            }
        }
        return true;
    }

    public static boolean compareTwo(Object o1, Object o2) {
        if (o1 == null && o2 == null) {
            return true;
        }
        if (o1 == null) {
            return false;
        }
        return o1.equals(o2);
    }

    public static boolean neCompareTwo(Object o1, Object o2) {
        return !compareTwo(o1, o2);
    }

    public static boolean neCompareIgnoreZero(Object o1, Object o2) {
        if ((o1 == null || (int)o1 == 0) && (o2 == null || (int)o2 == 0)) {
            return false;
        }
        return !compareTwo(o1, o2);
    }

    public static boolean compareListString(List<String> list1, List<String> list2) {
        if ((list1 == null || list1.size() == 0) && (list2 == null || list2.size() == 0)) {
            return true;
        }
        if (list1 == null || list2 != null) {
            return false;
        }
        if (list1.size() != list2.size()) {
            return false;
        }
        String[] arr1 = list1.toArray(new String[]{});
        String[] arr2 = list2.toArray(new String[]{});
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }

    /**
     * 比较字符串类型的数组 比如"[1,2,3]"
     * @param str1
     * @param str2
     * @return
     */
    public static boolean compareStringArray(String str1, String str2) {
        String[] arr1 = str1.replace("[","").replace("]","").split(",");
        String[] arr2 = str2.replace("[","").replace("]","").split(",");

        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }


    /**
     * Bigdecimal 字段比较
     * @param fee
     * @param oldFee
     * @return
     */
    public static boolean compareDecimal(BigDecimal fee, BigDecimal oldFee) {
        if (fee == null && oldFee == null) {
            return true;
        }
        if (fee == null) {
            return oldFee.compareTo(BigDecimal.ZERO) == 0;
        }
        if (oldFee == null) {
            return fee.compareTo(BigDecimal.ZERO) == 0;
        }
        return fee.compareTo(oldFee) == 0;
    }

    public static String objectToString(Object object) {
        if (object == null) {
            return "";
        } else {
            return object.toString();
        }
    }

    public static String paymentArrayToStr(Integer[] arr) {
        List<String> list = new ArrayList<>();
        for (Integer integer : arr) {
            list.add(PaymentTypeEnum.getValue(integer));
        }
        return list.toString();
    }

}
